package com.sizu.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sizu.common.dto.DamageListDto;
import com.sizu.common.dto.PurchaseListGoodsDto;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.TDamageList;
import com.sizu.entity.TDamageListGoods;
import com.sizu.service.GoodsService;
import com.sizu.service.ITDamageListGoodsService;
import com.sizu.service.ITDamageListService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 柳旦
 * @since 2020-02-28
 */
@RestController
@RequestMapping("/inventory/tDamageList")
public class TDamageListController {

    //注入远程调用的service
    @Autowired
    private GoodsService goodsService;

    //注入报损单明细的service
    @Autowired
    private ITDamageListGoodsService iTDamageListGoodsService;

    //注入报损单的service
    @Autowired
    private ITDamageListService iTDamageListService;


    //当点击添加时，feign调用的方法
    @RequestMapping("/goodsList")
    public ResultEntity goodsList(PurchaseListGoodsDto purchaseListGoodsDto){
        ResultEntity resultEntity = goodsService.purchaseGoodsList(purchaseListGoodsDto);
        return ResultEntity.ok(resultEntity);
    }

    //做报损单和报损单详情添加的方法
    @RequestMapping("/damageListAdd")
    public ResultEntity damageListAdd(DamageListDto damageListDto){
        TDamageList tDamageList = new TDamageList();
        BeanUtils.copyProperties(damageListDto,tDamageList);
        iTDamageListService.insert(tDamageList);//向订单表里添加数据
        //获取新添加进去的id
        Wrapper<TDamageList> wrapper = new EntityWrapper<TDamageList>(tDamageList);
        TDamageList tDamageList1 = iTDamageListService.selectOne(wrapper);
        if(null!=damageListDto.getDamageListGoodsJSON()){
            List<TDamageListGoods> tDamageListGoods = JSON.parseArray(damageListDto.getDamageListGoodsJSON(), TDamageListGoods.class);
            for (TDamageListGoods tDamageListGood : tDamageListGoods) {
                tDamageListGood.setDamageListId(tDamageList1.getId());
                Wrapper<TDamageListGoods> wrapper1 = new EntityWrapper<TDamageListGoods>();
                wrapper1.eq("name",tDamageListGood.getName());
                TDamageListGoods tDamageListGoods1 = iTDamageListGoodsService.selectOne(wrapper1);
                if(null!=tDamageListGoods1){
                    iTDamageListGoodsService.updateById(tDamageListGood);
                }
                iTDamageListGoodsService.insert(tDamageListGood);
            }
        }
        return ResultEntity.ok();
    }

    //做添加的方法（看似是添加，其实是修改的方法）
    @RequestMapping("/DamagegoodsAdd")
    public ResultEntity DamagegoodsAdd(PurchaseListGoodsDto purchaseListGoodsDto){
        ResultEntity resultEntity = goodsService.updatetPurchaseListGoods(purchaseListGoodsDto);
        return ResultEntity.ok(resultEntity);
    }

    @RequestMapping("/deleteDamagegoods")
    public ResultEntity deleteDamagegoods(PurchaseListGoodsDto purchaseListGoodsDto){
        ResultEntity resultEntity = goodsService.deletetPurchaseListGoods(purchaseListGoodsDto);
        return ResultEntity.ok(resultEntity);
    }
}
